home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / GameKit / Examples / NX_Invaders / NXIPlayer.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  2.8 KB  |  109 lines

  1.  
  2. // Handles moving and rendering the base, whether under player or demo control.
  3.  
  4. #import <libc.h>
  5. #import "NXIPlayer.h"
  6.  
  7. @implementation NXIPlayer
  8.  
  9. - init                        // initialize the player
  10. {
  11.     [super init];
  12.     playerStopped = NO;
  13.     renderedImage = [NXImage findImageNamed:"Base.tiff"];
  14.     NX_HEIGHT(&boundingBox) = BASE_WIDTH;
  15.     NX_WIDTH(&boundingBox) = BASE_HEIGHT;
  16.     maxFrames[0] = BASE_FRAMES; xdirection = 0.0;
  17.     // ***** need to set up keys for movement!
  18.     return self;
  19. }
  20.  
  21. - (BOOL)newPlayer            // get and set up a new base.  Returns NO if can't
  22. {
  23.     if (![basesLeft decNumUp:self]) return NO;        // no bases left
  24.     [self resetPlayer];
  25.     return YES;
  26. }
  27.  
  28. - resetPlayer                // reset all player info
  29. {
  30.     state = BASE_ALIVE;
  31.     xdirection = 0.0;
  32.     GK_SET_VECTOR(&nextLocation, 0.0, 0.0); // start in lower left corner
  33.     return self;
  34. }
  35.  
  36. - setStage:aStage
  37. {
  38.     id ret = [super setStage:aStage];
  39.     NXRect bounds;
  40.     [[aStage bufferType:GK_SCREEN_BUFFER] getBounds:&bounds];
  41.     // figure out how far to the right we can go
  42.     screenWidth = NX_WIDTH(&bounds) - BASE_WIDTH - BASE_STEPS;
  43.     return ret;
  44. }
  45.  
  46. - demoMove:sender    // handles base movement during the demo sequence
  47. {
  48.     return self;
  49. }
  50.  
  51. - move:sender                // Move the base one animation frame
  52. {
  53.     GK_VECTOR_X(&nextLocation) += xdirection;
  54.     GK_VECTOR_X(&nextLocation) = GK_CLAMP(GK_VECTOR_X(&nextLocation),
  55.             BASE_STEPS, (screenWidth - BASE_WIDTH - BASE_STEPS));
  56.     return self;
  57. }
  58.  
  59. - collidedWith:anActor    // called when it is detected that we hit something
  60. {    // we need to see if it was a vader's bullet that hit us (we ignore
  61.     // anything else...) and then act accordingly.
  62.     // *****
  63.     return self;
  64. }
  65.  
  66. - keyChanged:(NXEvent *)myevent :(int)kind
  67. {    // gameview will tell us one of these if a key was hit (or released)
  68.     if (kind == GK_ALL_KEYUP) {
  69.         xdirection = 0.0;
  70.         return self;
  71.     }
  72.     if ((kind == GK_KEYUP) &&
  73.             (GK_KEYS_ARE_SAME(myevent, &keys[NXI_LEFTKEY]) ||
  74.              GK_KEYS_ARE_SAME(myevent, &keys[NXI_RIGHTKEY]))) {
  75.         xdirection = 0.0;
  76.     } else if (kind == GK_KEYDOWN) {
  77.         if (GK_KEYS_ARE_SAME(myevent, &keys[NXI_LEFTKEY]))
  78.             xdirection = -BASE_STEPS;
  79.         if (GK_KEYS_ARE_SAME(myevent, &keys[NXI_RIGHTKEY]))
  80.             xdirection = BASE_STEPS;
  81.         if (GK_KEYS_ARE_SAME(myevent, &keys[NXI_FIREKEY])) {
  82.             // ***** fire a shot
  83.         }
  84.     }
  85.     return self;
  86. }
  87.  
  88. - updateDrawingState    // change the internal state machine (ie. advance
  89. {
  90.     id ret;
  91.     ret = [super updateDrawingState];
  92.     // wrap frames early for living base (there is only one frame)
  93.     if (state == BASE_ALIVE) frame = 0;
  94.     // if we've finished stepping through the death frames, then we need
  95.     // to get off the stage since the death is complete.
  96.     if ((state == BASE_DYING) && !frame) { // wraparound occurred
  97.         state = BASE_DEAD;
  98.         // ***** get a new base and end game if cannot
  99.     }
  100.     return ret;
  101. }
  102.  
  103. - setDirection:(int)newDirection;    // sent by GameView at right time
  104. {    // ***** not yet done
  105.     return self;
  106. }
  107.  
  108. @end
  109.